<template>
  <el-dialog
    title="查看用户详情"
    v-model="visible"
    top="10vh"
    width="70%"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="dataFormRef"
      :model="submitForm"
      label-position="right"
      label-width="120px"
      v-loading="loading"
    >
      <el-form-item label="发送邮件营销语" prop="emailMarketing">
        <el-input type="textarea" v-model="submitForm.emailMarketing"></el-input>
      </el-form-item>
      <el-form-item label="邮件内容" prop="content">
        <el-input type="textarea" v-model="submitForm.content"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="sendMailEvent">发送邮件</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch, type PropType } from 'vue'
import { UPDATE_MODEL_EVENT } from 'cm/contants'
import { ElMessage } from 'element-plus'
import { validPhone } from '@/utils/validate'

import { sendMail, type ICreateOrUpdateUser, type ICreateSendMail } from '@/api/clientUser'
import { getSetting, type ICreateOrUpdateSetting } from '@/api/setting';

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  },
  currEmail: {
    type: String,
    default: ''
  }
})

const emit = defineEmits([UPDATE_MODEL_EVENT, 'change'])

const visible = ref<boolean>(false)

const handleClose = () => {
  emit(UPDATE_MODEL_EVENT, false)
}

  const defaultSettingForm = {
    emailCode: '',
    emailMail: '',
    receiveMail: '',
    emailPort: 0,
    emailMarketing: '',
    smsAccessKey: '',
    smsAccessSecret: '',
    smsSignName: '',
    smsCode: '',
    ossAccessKey: '',
    ossAccessSecret: '',
    ossRegion: '',
    ossBucket: ''
  }
  // 表单逻辑
  const settingForm = ref<ICreateOrUpdateSetting>({ ...defaultSettingForm });

  const loading = ref<boolean>(false);

  const defaultForm = {
    receiveEmail: '',
    emailMarketing: '',
    content: '',
  }

  const submitForm = ref<ICreateSendMail>({ ...defaultForm });
  // 查询系统设置详情
  const getSettingApi = async () => {
    loading.value = true
    const res = await getSetting()
    loading.value = false
    if (res?.code === 200) {
      settingForm.value = res.data as ICreateOrUpdateSetting;
      submitForm.value.emailMarketing = settingForm.value.emailMarketing;
    }
  }
  
const dataFormRef = ref()

const sendMailEvent = () => {
  dataFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      const res = await sendMail(submitForm.value)
      loading.value = false
      if (res?.code === 200) {
        ElMessage({ type: 'success', message: '发送成功' })
        emit('change')
        close()
      } else {
        ElMessage({ type: 'error', message: res?.msg || '网络异常，请稍后重试！' })
      }
    }
  })
}

const close = () => {
  emit('change')
  handleClose()
}

watch(
  () => props.modelValue,
  async () => {
    visible.value = props.modelValue
    if (props.modelValue) {
      submitForm.value.receiveEmail = props.currEmail;
      console.log(submitForm.value)
      await getSettingApi()
    }
  }
)

</script>
